home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume90 / unix / du_1_2 / part01
Encoding:
Text File  |  1990-07-08  |  12.5 KB  |  445 lines

  1. Path: xanth!cs.odu.edu!Amiga-Request
  2. From: Amiga-Request@cs.odu.edu (Amiga Sources/Binaries Moderator)
  3. Newsgroups: comp.sources.amiga
  4. Subject: v90i201: du 1.2 - calculate disc block usage, Part01/01
  5. Message-ID: <13079@xanth.cs.odu.edu>
  6. Date: 8 Jul 90 15:28:30 GMT
  7. Sender: tadguy@cs.odu.edu
  8. Reply-To: peterc@softway.sw.oz.au (Peter Chubb)
  9. Lines: 431
  10. Approved: tadguy@cs.odu.edu (Tad Guy)
  11. X-Mail-Submissions-To: Amiga@cs.odu.edu
  12. X-Post-Discussions-To: comp.sys.amiga
  13.  
  14. Submitted-by: peterc@softway.sw.oz.au (Peter Chubb)
  15. Posting-number: Volume 90, Issue 201
  16. Archive-name: unix/du-1.2/part01
  17.  
  18. This is du, a utility to tell you how much disc space a directory tree or
  19. file is actually using.  Moreover, it can tell you how much space the
  20. directory would take if transferred to a floppy using the slow file system,
  21. or if transferred to a hard disc using the fast file system.
  22. It is very small, and is an example of self-compiling code that does not
  23. need a startup routine.
  24.  
  25. #!/bin/sh
  26. # This is a shell archive.  Remove anything before this line, then unpack
  27. # it by saving it into a file and typing "sh file".  To overwrite existing
  28. # files, type "sh file -c".  You can also feed this as standard input via
  29. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  30. # will see the following message at the end:
  31. #        "End of archive 1 (of 1)."
  32. # Contents:  du.c du.doc du.uu
  33. # Wrapped by tadguy@xanth on Sun Jul  8 11:28:21 1990
  34. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  35. if test -f 'du.c' -a "${1}" != "-c" ; then 
  36.   echo shar: Will not clobber existing file \"'du.c'\"
  37. else
  38. echo shar: Extracting \"'du.c'\" \(5487 characters\)
  39. sed "s/^X//" >'du.c' <<'END_OF_FILE'
  40. X; /* To make, just execute me
  41. Xlc -v -j73 -O -cusf -M du
  42. Xblink du.o lib lib:lc.lib nd sc sd batch quiet
  43. Xprotect du +p
  44. Xquit
  45. X*/
  46. X/************************************************************************
  47. X * du.c -- calculate disk usage for files in a directory tree        *
  48. X *                                    *
  49. X *        Copyright 1989,1990 Peter Chubb                *
  50. X *           All rights reserved                    *
  51. X *                                    *
  52. X ************************************************************************/
  53. X
  54. Xstatic char RCSid[] = "$Id: du.c,v 1.2 90/06/06 20:07:37 peterc Exp $";
  55. X
  56. X/*
  57. X * Written:
  58. X *    10 July 1989 Peter Chubb
  59. X * $Log:    du.c,v $
  60. X * Revision 1.2  90/06/06  20:07:37  peterc
  61. X * Added options to allow selection of blocksize;
  62. X * added allowance for file extension blocks.
  63. X * 
  64. X *
  65. X */
  66. X
  67. X#include <libraries/dos.h>
  68. X#include <proto/dos.h>
  69. X#include <proto/exec.h>
  70. X#include <exec/memory.h>
  71. X#include <string.h>
  72. X#include <stdlib.h>
  73. X#include <dos.h>
  74. X
  75. X#ifndef min
  76. X#    define min(a, b) ((a) < (b) ? (a) : (b))
  77. X#endif
  78. X
  79. X#define ABSEXECBASE ((struct ExecBase **)4L)
  80. X
  81. Xvoid RawDoFmt(char *fmt, void *arglist, void (*prbuf)(), char *obuf);
  82. X#pragma syscall RawDoFmt 20a ba9804
  83. X
  84. Xvoid __asm prbuf(register __d0 char c);
  85. X#define R_A3 (8+3)
  86. X
  87. Xstruct DosLibrary *DOSBase;
  88. X
  89. Xvoid outval(char *fmt,...);
  90. X
  91. Xlong __regargs du(BPTR dir, long slnt, unsigned long level, unsigned long bsize);
  92. Xlong __asm __saveds dumain(register __a0 char *cmd);
  93. Xunsigned long numBlocks(LONG size, unsigned long bsize);
  94. X
  95. X
  96. X/* main routine -- parse arguments, then call du() to do the work */
  97. Xlong __asm __saveds
  98. Xdumain(register __a0 char *cmd)
  99. X{
  100. X    long total;
  101. X    register char *p;
  102. X    long       c;
  103. X    long      slnt = 0;
  104. X    BPTR      dir;
  105. X    struct Library *foo;
  106. X    unsigned long bsize = 0;
  107. X    struct InfoData infodata;
  108. X
  109. X    if (!(foo = OpenLibrary("dos.library", 0)))
  110. X        return RETURN_ERROR;
  111. X    DOSBase = (struct DosLibrary *) foo;
  112. X
  113. X    while ((c = *cmd++) == ' ')
  114. X    ;
  115. X
  116. X    while (c == '-')
  117. X    {
  118. X        c = *cmd++;
  119. X    switch(c)
  120. X    {
  121. X    case 's':    
  122. X        slnt = 1;
  123. X        break;
  124. X
  125. X    case 'F':
  126. X        if (bsize)
  127. X        {
  128. X            outval ("File system blocksize specified twice!\n");
  129. X            return RETURN_ERROR;
  130. X        }
  131. X        bsize = 512;
  132. X        break;
  133. X
  134. X    case 'S':
  135. X        if (bsize)
  136. X        {
  137. X            outval ("File system blocksize specified twice!\n");
  138. X            return RETURN_ERROR;
  139. X        }
  140. X        bsize = 488;
  141. X        break;
  142. X
  143. X    default:
  144. X        outval("Usage: du [-s] [-F | -S] [dirname]\n");
  145. X        return RETURN_ERROR;
  146. X    }
  147. X    while ((c = *cmd++) == ' ')
  148. X        ;
  149. X    } 
  150. X    p = --cmd;
  151. X    while (*p != ' ' && *p != '\n' && *p)
  152. X    p++;
  153. X    *p = '\0';
  154. X
  155. X    if (p != cmd) 
  156. X    {
  157. X    c = 1;
  158. X    dir = Lock(cmd, ACCESS_READ);
  159. X    }
  160. X    else 
  161. X    {
  162. X    c = 0;
  163. X    dir = CurrentDir(0L);
  164. X    (void) CurrentDir(dir);
  165. X    }
  166. X    if (!dir) 
  167. X    {
  168. X        outval("Can't open %ls\n", cmd);
  169. X    return RETURN_WARN;
  170. X    }
  171. X
  172. X    if (!bsize)
  173. X    {
  174. X    Info(dir, &infodata);
  175. X    bsize = infodata.id_BytesPerBlock;
  176. X    }
  177. X
  178. X    total = du(dir, slnt, 0, bsize);
  179. X
  180. X    if (c)
  181. X    UnLock(dir);
  182. X
  183. X    if (total >= 0)
  184. X        outval("Total: %ld\n", total);
  185. X    else
  186. X    outval("**BREAK**\n\n");
  187. X
  188. X    CloseLibrary(foo);
  189. X    return RETURN_OK;
  190. X}
  191. X
  192. X
  193. X/* outval -- our equivalent of printf */
  194. Xvoid
  195. Xoutval(char *fmt, ...)
  196. X{
  197. X    char obuf[200];
  198. X   
  199. X    RawDoFmt(fmt, (&fmt) + 1, prbuf, obuf);
  200. X    Write(Output(), obuf, strlen(obuf));
  201. X}
  202. X
  203. X/* du -- calculate and print disc usage */
  204. Xlong __regargs
  205. Xdu(dir, slnt, level, bsize)
  206. XBPTR dir; /* lock on top of directory tree */
  207. Xlong slnt; /* whether to print or not */
  208. Xunsigned long level; /* how deep in the tree */
  209. Xunsigned long bsize;
  210. X{
  211. X    /* note all these are longword aligned */
  212. X
  213. X    struct FileInfoBlock fib;
  214. X    BPTR       lck;
  215. X    long    total = 0;
  216. X    long    extra;
  217. X    BPTR       curdir = CurrentDir(dir);
  218. X    long    abort = 0;
  219. X
  220. X    if (Examine(dir, &fib)) {
  221. X    if (fib.fib_DirEntryType < 0) {
  222. X        if (!slnt) {
  223. X        Write(Output(), "                                  ", 
  224. X                (long)min(level, 32));
  225. X            outval("%ls    %ld\n", fib.fib_FileName, 
  226. X                total = numBlocks(fib.fib_Size, bsize));
  227. X        }
  228. X    } else 
  229. X        while (ExNext(dir, &fib) & abort >= 0) {
  230. X            if (SetSignal(0, 0) & SIGBREAKF_CTRL_C)
  231. X            abort = -1;
  232. X            else {
  233. X            extra = numBlocks(fib.fib_Size, bsize);
  234. X                if (fib.fib_DirEntryType > 0) {
  235. X            lck = Lock(fib.fib_FileName, ACCESS_READ);
  236. X            extra += (abort = du(lck, slnt, level+1, bsize));
  237. X                UnLock(lck);
  238. X            } 
  239. X            total += extra;
  240. X                if (!slnt && abort >= 0) {
  241. X            Write(Output(), "                                  ",
  242. X                    min(level, 32));
  243. X                    outval("%-32ls %6ld\n", fib.fib_FileName, extra);
  244. X                }
  245. X                }
  246. X            }
  247. X    }
  248. X    (void) CurrentDir(curdir);
  249. X    return abort < 0 ? -1 : total;
  250. X}
  251. X
  252. X
  253. X
  254. X/*----------------------------------------------------------------*/
  255. X/* This stub routine is called from the RawDoFmt routine for each */
  256. X/* character in the string.  At invocation, we have:              */
  257. X/*   D0 - next character to be formatted                          */
  258. X/*   A3 - pointer to data buffer                                  */
  259. X/* Stolen from Lattice-supplied Avail utility              */
  260. X/*----------------------------------------------------------------*/
  261. Xvoid __asm prbuf(register __d0 char c)
  262. X{
  263. X    char *p = (char *)__builtin_getreg(R_A3);
  264. X    *p++ = c;
  265. X    __builtin_putreg(R_A3, (long)p);
  266. X
  267. X    /* It's a pity this doesn't generate
  268. X     *      move.b d0,(a3)+
  269. X     *   rts
  270. X     */
  271. X}
  272. X
  273. X/* numBlocks -- work out how many blocks a file takes */
  274. Xunsigned long
  275. XnumBlocks(LONG size, unsigned long bsize)
  276. X{
  277. X    register unsigned long    blocks;
  278. X    
  279. X    blocks = (size + bsize - 1) / bsize;
  280. X    blocks += (blocks / ((bsize/4) - 56)) + 1;
  281. X    
  282. X    return blocks;
  283. X}
  284. X
  285. END_OF_FILE
  286. if test 5487 -ne `wc -c <'du.c'`; then
  287.     echo shar: \"'du.c'\" unpacked with wrong size!
  288. fi
  289. # end of 'du.c'
  290. fi
  291. if test -f 'du.doc' -a "${1}" != "-c" ; then 
  292.   echo shar: Will not clobber existing file \"'du.doc'\"
  293. else
  294. echo shar: Extracting \"'du.doc'\" \(1862 characters\)
  295. sed "s/^X//" >'du.doc' <<'END_OF_FILE'
  296. XDU            AMIGA            DU
  297. X
  298. XNAME
  299. X    du -- print out disc usage
  300. X
  301. XSYNOPSIS
  302. X    du [ -s ] [ -S | -F ] [ filename ]
  303. X
  304. XDESCRIPTION
  305. X
  306. X    du prints out a summary of disc usage  for
  307. X    a file or directory.  It is pure, and thus
  308. X    can  be  made  resident.  It takes up only
  309. X    1436 bytes.
  310. X    
  311. X    The options to du are:
  312. X    
  313. X    -s  short  listing.   Print only the total
  314. X        number of blocks used.
  315. X
  316. X    -S  Calculate the number of blocks used as
  317. X        if by the old (Slow) 488 byte-block
  318. X        file system.
  319. X
  320. X    -F  Calculate the number of blocks used as
  321. X        if  by  the  new (Fast) 512 byte block
  322. X        file system.
  323. X
  324. X    Using the -S and -F options, one can easily
  325. X    work  out whether a directory will fit when
  326. X    moved between floppy and hard disc.
  327. X    Without either option, du interrogates  the
  328. X    file system to work out the blocksize.
  329. X
  330. XCAVEATS
  331. X    du correctly  accounts for directory blocks
  332. X    and  file  list  blocks  on  AmigaDOS  file
  333. X    systems.    It   will  work  incorectly  on
  334. X    MS-DOS volumes mounted via msh: or crossdos. 
  335. X
  336. XAUTHOR
  337. X    Peter Chubb
  338. X    peterc@softway.sw.oz.au
  339. X
  340. XDISTRIBUTION
  341. X
  342. X
  343. X    Copyright 1990 Peter Chubb
  344. X    All rights reserved.
  345. X
  346. X    This program and its associated
  347. X    documentation may not be distributed for
  348. X    profit.  It may be distributed provided 
  349. X    
  350. X    a) no charge is made other than for
  351. X    reasonable copying and media expenses, 
  352. X
  353. X    b) no change is made to the source,
  354. X    documentation or binary, that is not
  355. X    clearly marked as being a change, and 
  356. X
  357. X    c) all files are provided.  These comprise:
  358. X        du.doc -- this documentation file
  359. X        du     -- the program binary
  360. X        du.c   -- the program source.
  361. X
  362. X    This program is not warranted, or
  363. X    guaranteed. You get exactly what you paid
  364. X    for -- a copy of the program to do as you
  365. X    wish with.  If it crashes your machine,
  366. X    writes rude letters to your spouse, or
  367. X    explodes in your face ... caveat emptor!
  368. X    However, to the best of my knowledge and
  369. X    belief it works as advertised.  
  370. END_OF_FILE
  371. if test 1862 -ne `wc -c <'du.doc'`; then
  372.     echo shar: \"'du.doc'\" unpacked with wrong size!
  373. fi
  374. chmod +x 'du.doc'
  375. # end of 'du.doc'
  376. fi
  377. if test -f 'du.uu' -a "${1}" != "-c" ; then 
  378.   echo shar: Will not clobber existing file \"'du.uu'\"
  379. else
  380. echo shar: Extracting \"'du.uu'\" \(2042 characters\)
  381. sed "s/^X//" >'du.uu' <<'END_OF_FILE'
  382. Xbegin 700 du
  383. XM```#\P`````````"``````````$```%(````#0```^D```%(3E7_O$CG+SA)!
  384. XM^0`````K2/_`)FW_P'@`>@!#^@%<<``L>``$3J[]V"]``"!*@&8&<`I@``$Z,
  385. XM*6\`(``P?@`>&W`@OH!G]F!F<``0&W)&D(%G$'(-D(%G)'(@D(%F.'@!8$)*L
  386. XMA6<.2'H!'&$``<AP"F```/PJ/````@!@*$J%9PY(>@$"80`!KG`*8```XBH\$
  387. XM```!Z&`.2'H!%&$``9AP"F```,Q^`!X;<""^@&?V<"V^@&>4)$M3BB9*8`)2V
  388. XMBQ`3<B"P`6<*<@JP`6<$2@!F[$(3M\IG$GP!(@IT_BQL`#!.KO^L+@!@%'P`-
  389. XM<@`L;``P3J[_@BX`(@=.KO^"2H=F#B\*2'H`S&$``2QP!6!@2H5F%"('0>W_Y
  390. XMQ"0(+&P`,$ZN_XXJ+?_8+P5"IR`'(@1A``%(4$\J`$J&9PHB!RQL`#!.KO^F?
  391. XM2H5K#B\%2'H`E&$``.103V`*2'H`E&$``-A83R)O`"`L>``$3J[^8G``3.T<0
  392. XM]/^<3EU.=61O<RYL:6)R87)Y`$9I;&4@<WES=&5M(&)L;V-K<VEZ92!S<&5C<
  393. XM:69I960@='=I8V4A"@!5<V%G93H@9'4@6RUS72!;+48@?"`M4UT@6V1I<FYA8
  394. XM;65="@!#86XG="!O<&5N("5L<PH`5&]T86PZ("5L9`H`*BI"4D5!2RHJ"@H`D
  395. XM("`@("`@("`@("`@("`@("`@("`@("`@("`@("`@("`@(```)6QS"25L9`H`,
  396. XM`"4M,S)L<R`E-FQD"@``3E7_.$CG,#`@;0`(0^T`#$7Z`<!'[?\X+'@`!$ZNT
  397. XM_?8L;``P3J[_Q"!+2AAF_%.(D<LB`"0+)@A.KO_03-\,#$Y=3G5.5?[<2.<_&
  398. XM`"XM``A"K?[L*T#^Y"M!_N@B`"QL`#!.KO^">``O0``8(BW^Y$'M_O`D"$ZNV
  399. XM_YI*@&<``30@+?[T2H!J``$(2JW^Z&8``2).KO_$+`!P(+Z`9`0B!V`"(@`O>
  400. XM00`<(@9!^O\8)`@F+P`<3J[_T"\M``PO+?]L80`!("Z`2&W^^$AZ_QPK0/[L[
  401. XM80#_+$_O`!!@``#6<``B`"QX``1.KO[."```#&<&>/]@``"<+RT`#"\M_VQA6
  402. XM``#B4$\L`"HM_N@@+?[T2H!O-D'M_O@B"'3^+&P`,$ZN_ZPB!U*!+RT`#"\!;
  403. XM+T``)"(%80#_$%!/*`#<A"(O`!PL;``P3J[_IMVM_NQ*A69`2H1K/"QL`#!.;
  404. XMKO_$*@!P(+Z`9`0B!V`"(@`O00`<(@5!^OY2)`@F+P`<3J[_T"\&2&W^^$AZ&
  405. XM_FQA`/YV3^\`#"(M_N1![?[P)`@L;``P3J[_E$J$6L%$`4B!2,'`@68`_RXB/
  406. XM+P`8+&P`,$ZN_X)*A&H$</]@!"`M_NQ,WP#\3EU.=2\'+@`6AT'K``$F2"X?0
  407. XM3G5.5?_X+P(@+0`,(@#DB70XDH(D+0`(U(!3@B`"+T$`!"(M``Q.N@!2(B\`4
  408. XM!"]```A.N@!&4H`B+P`(TH`@`20?3EU.=0``2H!J```>1(!*@6H```Q$@6$`5
  409. XM`"!$@4YU80``&$2`1(%.=4J!:@``#$2!80``!D2`3G4O`DA!-`%F```B2$!(H
  410. XM04A"-`!G```&A,$P`DA`-`"$P3`"2$(R`B0?3G4O`W80#$$`@&0```;AF5%#7
  411. XM#$$(`&0```;IF5E##$$@`&0```;EF55#2D%K```&XYE30S0`YJA(0D)"YJI(`
  412. XM0X#!-@`P`C0#2$'$P9""9```"%-#T(%D_G(`,@-(0^>X2$#!028?)!].=0``>
  413. XM`^P````!`````0````H````````#\@```^H````-)$ED.B!D=2YC+'8@,2XR2
  414. XI(#DP+S`V+S`V(#(P.C`W.C,W('!E=&5R8R!%>'`@)````````````_(@#
  415. X``
  416. Xend
  417. Xsize 1436
  418. END_OF_FILE
  419. if test 2042 -ne `wc -c <'du.uu'`; then
  420.     echo shar: \"'du.uu'\" unpacked with wrong size!
  421. fi
  422. # end of 'du.uu'
  423. fi
  424. echo shar: End of archive 1 \(of 1\).
  425. cp /dev/null ark1isdone
  426. MISSING=""
  427. for I in 1 ; do
  428.     if test ! -f ark${I}isdone ; then
  429.     MISSING="${MISSING} ${I}"
  430.     fi
  431. done
  432. if test "${MISSING}" = "" ; then
  433.     echo You have the archive.
  434.     rm -f ark[1-9]isdone
  435. else
  436.     echo You still need to unpack the following archives:
  437.     echo "        " ${MISSING}
  438. fi
  439. ##  End of shell archive.
  440. exit 0
  441. -- 
  442. Mail submissions (sources or binaries) to <amiga@cs.odu.edu>.
  443. Mail comments to the moderator at <amiga-request@cs.odu.edu>.
  444. Post requests for sources, and general discussion to comp.sys.amiga.
  445.